﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetTookKits.IOCP
{
    public class LocalBuffer
    {
        #region 构造方法
        public LocalBuffer(int bufferSize)
        {
            m_localbuffer = new Byte[bufferSize];
            m_localbufferCount = 0;
        }
        #endregion
        #region 本地接收缓存
        protected byte[] m_localbuffer;
        int m_localbufferCount;
        public byte[] LocalRecvBuffer
        {
            get
            {
                return m_localbuffer;
            }
        }
        public int LocalBufferCount
        {
            get
            {
                return m_localbufferCount;
            }
        }
        public void pushData(byte[] srcData, int srcIdx, int length)
        {
            if ((m_localbuffer.Length) < (m_localbufferCount + length))
            {
                m_localbufferCount = 0;
            }
            Array.Copy(srcData, srcIdx, m_localbuffer, m_localbufferCount, length);
            m_localbufferCount += length;
        }
        public void keepCount(int keeplen)
        {
            if (keeplen >= m_localbuffer.Length)
            {
                m_localbufferCount = 0;
                Array.Clear(m_localbuffer, 0, m_localbuffer.Length);
            }
            else if (keeplen == 0)
            {
                m_localbufferCount = 0;
                Array.Clear(m_localbuffer, 0, m_localbuffer.Length);
            }
            else
            {
                Array.Copy(m_localbuffer, m_localbufferCount - keeplen, m_localbuffer, 0, keeplen);
                m_localbufferCount = keeplen;
            }
        }
        #endregion
    }
}
